package com.hl.springboot4.controller;

import com.hl.springboot4.entity.Student;
import com.hl.springboot4.entity.User;
import com.hl.springboot4.service.UserService;
import com.hl.springboot4.util.R;
import com.sun.org.glassfish.external.statistics.annotations.Reset;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.*;
import java.util.UUID;

/**
 * @ClassName UserController
 * @Description TODO
 * @Author libowen
 * @Date 2025年03月03日 17:38
 * @Version 1.0
 */
@RestController
@RequestMapping("user")
@Validated   // 开启校验
@Api(tags = "用户模块")
@Slf4j
//@CrossOrigin
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("/test")
    public R test(){
        log.error("错误日志");
        log.warn("警告日志");
        log.info("测试日志");
        log.debug("调试日志");
        log.trace("trace日志");
        return R.ok("测试成功");
    }
    @ApiOperation(value = "测试方法")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(value = "姓名",name = "name",required = true),
            @ApiImplicitParam(value = "年龄",name = "age",required = true),
            @ApiImplicitParam(value = "电话",name = "tel",required = true),
            @ApiImplicitParam(value = "邮箱",name = "email",required = true)
    })
    @ApiResponses({
            @ApiResponse(code = 200,message = "成功"),
            @ApiResponse(code = 400,message = "参数错误"),
            @ApiResponse(code = 500,message = "服务器错误")
    })
    @GetMapping("/test1")
    public R test1(  @NotBlank @Size(min = 2,max = 20) String name,
                     @Min(0) @Max(150) int age,
                     @Pattern(regexp = "^[1-9][0-9]{10}$") String tel,
                     @Email String email) {
        System.out.println(name+age+tel+email);
        System.out.println("----------校验成功才可以执行-----------");
        return R.ok("校验成功");
    }

    @ApiOperation(value = "测试方法2")
    @ApiResponses({
            @ApiResponse(code = 200,message = "成功"),
            @ApiResponse(code = 400,message = "参数错误"),
            @ApiResponse(code = 500,message = "服务器错误")
    })
    @GetMapping("/test2")
    public R test2( @Valid Student student){
        System.out.println(student.toString());
        return R.ok("校验成功");
    }



    //登录
    @GetMapping("login")
    public R login(String username,String password){
        int code = userService.login(username, password);
        if(code==1){
            return R.ok("登录成功");
        }else if(code==-1){
            return R.fail("用户名不存在");
        }else {
            return R.fail("密码错误");
        }
    }

    @GetMapping("login2")
    public R login2(String username,String password){
        User user = userService.login2(username, password);
        if(user!=null){
            R r = R.ok(user);
            r.setMsg(UUID.randomUUID().toString().replace("-","").substring(0,6)); // token
            return r;
        }else {
            return R.fail("用户名或密码错误");
        }
    }


    // get请求
    @GetMapping("getUser")  // localhost:8080/user/getUser?id=1
    public R getUser(int id){
        return R.ok("哈哈"+id);
    }

    @GetMapping("/{id}")  // localhost:8080/user/12
    public R getUser2(@PathVariable("id") int id){
        return R.ok("嘻嘻"+id);
    }

    @DeleteMapping("delUser")  // localhost:8080/user/delUser?id=1
    public R delUser(int id){
        return R.ok("哈哈"+id);
    }

    @DeleteMapping("/{id}")  // localhost:8080/user/12
    public R delUser2(@PathVariable("id") int id){
        return R.ok("嘻嘻"+id);
    }

}
